package com.cssw.fyzb.core.dao.user;

import com.cssw.fyzb.core.model.dodb.UserExt;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

@Repository
public class UserExtDaoImpl implements UserExtDao {
    @Override
    public void save(Long userId) {
        UserExt userExt = new UserExt();
        userExt.setUserId(userId);
        userExt.setAddActiveNum(0);
        userExt.setActiveNum(0);
        userExt.save();
    }

    @Override
    public UserExt get(Long userId) {
        return UserExt.dao.findById(userId);
    }

    @Override
    public void setValid(Long userId) {
        UserExt userExt = get(userId);
        if (userExt != null) {
            if (userExt.getValidUser() == UserExt.VALID_STATUS) {
                return;
            }
            userExt.setValidUser(UserExt.VALID_STATUS);
            userExt.update();
        }
    }

    @Override
    public void incActiveNum(Long userId, Integer activeNum) {
        UserExt userExt = get(userId);
        if (userExt != null) {
            userExt.setActiveNum(userExt.getActiveNum() + activeNum);
            userExt.update();
        }

    }

    @Override
    public void decActiveNum(Long userId, Integer activeNum) {
        UserExt userExt = get(userId);
        if (userExt != null) {
            userExt.setActiveNum(userExt.getActiveNum() - activeNum);
            userExt.update();
        }
    }

    @Override
    public void incFansNum(Long userId, Integer fansNum) {
        UserExt userExt = get(userId);
        if (userExt != null) {
            userExt.setFans(userExt.getFans() + fansNum);
            userExt.update();
        }
    }

    @Override
    public void decFansNum(Long userId, Integer fansNum) {
        UserExt userExt = get(userId);
        if (userExt != null) {
            userExt.setFans(userExt.getFans() - fansNum);
            userExt.update();
        }
    }

    @Override
    public void incFriendsNum(Long userId, Integer friendsNum) {
        UserExt userExt = get(userId);
        if (userExt != null) {
            userExt.setFriends(userExt.getFriends() + friendsNum);
            userExt.update();
        }
    }

    @Override
    public void decFriendsNum(Long userId, Integer friendsNum) {
        UserExt userExt = get(userId);
        if (userExt != null) {
            userExt.setFriends(userExt.getFriends() - friendsNum);
            userExt.update();
        }
    }

    @Override
    public void incProductionsNum(Long userId, Integer productionsNum) {
        UserExt userExt = get(userId);
        if (userExt != null) {
            if (null == userExt.getProductions()) {
                userExt.setProductions(productionsNum);
            } else {
                userExt.setProductions(userExt.getProductions() + productionsNum);
            }
            userExt.update();
        }
    }

    @Override
    public void decProductionsNum(Long userId, Integer productionsNum) {
        UserExt userExt = get(userId);
        if (userExt != null) {
            userExt.setProductions(userExt.getProductions() - productionsNum);
            userExt.update();
        }
    }

    @Override
    public void incLikesNum(Long userId, Integer likesNum) {
        UserExt userExt = get(userId);
        if (userExt != null) {
            if (null == userExt.getLikes()) {
                userExt.setLikes(likesNum);
            } else {
                userExt.setLikes(userExt.getLikes() + likesNum);
            }
            userExt.update();
        }
    }

    @Override
    public void decLikesNum(Long userId, Integer likesNum) {
        UserExt userExt = get(userId);
        if (userExt != null) {
            userExt.setLikes(userExt.getLikes() - likesNum);
            userExt.update();
        }
    }

    @Override
    public void incAddActiveNum(Long userId, Integer addActiveNum) {
        UserExt userExt = get(userId);
        if (userExt != null) {
            if (null == userExt.getActiveNum()) {
                userExt.setActiveNum(addActiveNum);
            } else {
                userExt.setActiveNum(userExt.getActiveNum() + addActiveNum);
            }
            userExt.update();
        }
    }

    @Override
    public void decAddActiveNum(Long userId, Integer addActiveNum) {
        UserExt userExt = get(userId);
        if (userExt != null) {
            userExt.setAddActiveNum(userExt.getAddActiveNum() - addActiveNum);
            userExt.update();
        }
    }

    @Override
    public void incTotalSendPack(Long userId, Integer totalSendPack) {
        UserExt userExt = get(userId);
        if (userExt != null) {
            if (userExt.getTotalSendPack() == null) { //红包个数为空时 默认为1
                userExt.setTotalSendPack(1);
            } else {
                userExt.setTotalSendPack(userExt.getTotalSendPack() + totalSendPack);
            }
            userExt.update();
        }
    }

    @Override
    public void incSumSendPack(Long userId, BigDecimal sumSendPack) {
        UserExt userExt = get(userId);
        if (userExt != null) {
            if (userExt.getSumSendPack() == null) { //红包个数为空时 默认为1
                userExt.setSumSendPack(sumSendPack);
            } else {
                userExt.setSumSendPack(userExt.getSumSendPack().add(sumSendPack));
            }
            userExt.update();
        }
    }

    @Override
    public void incTotalRecvPack(Long userId, Integer totalRecvPack) {
        UserExt userExt = get(userId);
        if (userExt != null) {
            if (null == userExt.getTotalRecvPack()) {
                userExt.setTotalRecvPack(totalRecvPack);
            } else {
                userExt.setTotalRecvPack(userExt.getTotalRecvPack() + totalRecvPack);
            }
            userExt.update();
        }
    }

    @Override
    public void incSumRecvPack(Long userId, BigDecimal sumRecvPack) {
        UserExt userExt = get(userId);
        if (userExt != null) {
            if (null == userExt.getSumRecvPack()) {
                userExt.setSumRecvPack(sumRecvPack);
            } else {
                userExt.setSumRecvPack(userExt.getSumRecvPack().add(sumRecvPack));
            }
            userExt.update();
        }
    }

    @Override
    public void updateStatis(Long userId, Integer activeNum, Integer fansNum, Integer friendsNum, Integer productionsNum, Integer likesNum, Integer addActiveNum) {
        UserExt userExt = UserExt.dao.findById(userId);
        if (userExt != null) {
            Integer oldData;
            if (activeNum != null) {
                userExt.setActiveNum(activeNum + userExt.getActiveNum());
            }
            if (fansNum != null) {
                userExt.setFans(fansNum + userExt.getFans());
            }
            if (friendsNum != null) {
                userExt.setFriends(friendsNum + userExt.getFriends());
            }
            if (productionsNum != null) {
                userExt.setProductions(productionsNum + userExt.getProductions());
            }
            if (likesNum != null) {
                userExt.setLikes(likesNum + userExt.getLikes());
            }
            if (addActiveNum != null) {
                userExt.setAddActiveNum(addActiveNum + userExt.getAddActiveNum());
            }

            userExt.update();
        }
    }

    @Override
    public void addGzAddPrice(Long userId, BigDecimal gzAddPrice) {
        UserExt userExt = UserExt.dao.findById(userId);
        if (userExt != null) {
            if (userExt.getGzAddPrice() != null) {
                userExt.setGzAddPrice(userExt.getGzAddPrice().add(gzAddPrice));
            } else {
                userExt.setGzAddPrice(gzAddPrice);
            }

            userExt.update();
        } else {
            //如果果子增值价格不存在，需要新增记录
            userExt = new UserExt();
            userExt.setUserId(userId);
            userExt.setAddActiveNum(0);
            userExt.setActiveNum(0);
            userExt.setTotalRecvPack(0);
            userExt.setTotalSendPack(0);
            userExt.setSumRecvPack(BigDecimal.ZERO);
            userExt.setSumSendPack(BigDecimal.ZERO);
            userExt.setGzAddPrice(gzAddPrice);
            userExt.save();
        }
    }
}
